package cn.yj.core.business.service;

import cn.yj.annotation.pagehelper.page.Page;
import cn.yj.api.FileUploadHandler;
import cn.yj.cgs.dba.entity.Swiper;
import cn.yj.cgs.dba.mapper.SwiperMapper;
import cn.yj.common.AppExecutor;
import cn.yj.common.Enums;
import cn.yj.common.ImageUtils;
import cn.yj.common.UUIdUtils;
import cn.yj.commons.StringPools;
import cn.yj.commons.utils.StringUtils;
import cn.yj.entity.Base64Entity;
import cn.yj.params.check.annotation.CheckObjectValue;
import cn.yj.params.check.annotation.KeyValue;
import cn.yj.params.check.annotation.Require;
import cn.yj.tools.exception.ServiceException;
import cn.yj.tools.readconfig.PropertiesUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 永健
 * @since 2021-06-20 14:49
 */
@Service
public class SwiperService extends ServiceImpl<SwiperMapper, Swiper> {
    public Page<Swiper> pageList(Map<String, Object> params, Page<Swiper> page) {
        baseMapper.pageList(params, page);
        return page;
    }

    public List<Swiper> listChoose() {
        return baseMapper.listChoose();
    }

    public boolean setChoose(@Require Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            Swiper swiper = new Swiper();
            swiper.setId(ids[i]);
            swiper.setChoose(Enums.Common.TRUE);
            baseMapper.updateById(swiper);
        }
        return true;
    }

    public boolean removeChoose(@Require Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            Swiper swiper = new Swiper();
            swiper.setId(ids[i]);
            swiper.setChoose(Enums.Common.FALSE);
            baseMapper.updateById(swiper);
        }
        return true;
    }


    private static File WATER_FILE;
    static {
        WATER_FILE = new File(PropertiesUtils.getStringValue("school-cbd.water-img-path"));
    }

    @Override
    @CheckObjectValue(keyValue = @KeyValue(type = Map.class, name = {"imgUrl"}))
    public boolean save(Swiper swiper) {
        String imgUrl = swiper.getImgUrl();
        String fileName =
                Enums.Minio.Bucket.swiper.name().concat("/").concat(UUIdUtils.getCode().concat(StringPools.JPG));
        try {
            // 水印处理
            imgUrl = ImageUtils.addImageWaterMark(imgUrl, WATER_FILE);
        } catch (IOException e) {
            log.error("水印处理异常",e);
        }

        try {
            String upload = FileUploadHandler.getInstant().upload(new Base64Entity(imgUrl, fileName));
            swiper.setImgUrl(upload);
            super.save(swiper);
        } catch (Exception e) {
            throw new ServiceException("图片上传异常,请联系管理员");
        }
        return true;
    }

    public boolean removeById(@Require Integer[] ids) {
        AppExecutor.exec(() -> {
            for (int i = 0; i < ids.length; i++) {
                Integer id = ids[i];
                Swiper swiper = baseMapper.selectById(id);
                if (StringUtils.isNull(swiper)) {
                    throw new ServiceException("该图片不存在");
                }
                String imgUrl = swiper.getImgUrl();
                try {
                    FileUploadHandler.getInstant().delete(imgUrl);
                } catch (Exception e) {
                     e.printStackTrace();
                }
            }
        });
        return super.removeByIds(Arrays.asList(ids));
    }
}
